package com.android.view.hqview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;


import com.android.R;

import java.util.Arrays;

public class IndexView extends LinearLayout implements RadioGroup.OnCheckedChangeListener {
    public  RadioGroup mRgAdjustType;
    public  RadioGroup mRgIndexType;
    public  OnIndexSelected mOnIndexSelected;

    private IndexModel adjustModel;
    private IndexModel indexModel;
    private int[] ids;
    private int selectIndex;
    public  int[] adjustIds;

    public IndexView(Context context) {
        super(context);
        init(context);
    }

    public IndexView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public IndexView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        inflate(context, R.layout.indexview, this);

        mRgAdjustType = (RadioGroup) findViewById(R.id.rgAdujstType);
        mRgIndexType = (RadioGroup) findViewById(R.id.rgIndexType);

        initAdjustType();
        initIndexListType();

        mRgAdjustType.setOnCheckedChangeListener(this);
        mRgIndexType.setOnCheckedChangeListener(this);
    }

    private void initIndexListType() {
        int first = 0;
        ids = new int[IndexModel.INDEXTYPES.length];
        for (int i = 0; i < IndexModel.INDEXTYPES.length; i++) {

            RadioButton btn = (RadioButton) View.inflate(getContext(), R.layout.indexitem, null);


            IndexModel m = new IndexModel(IndexModel.INDEXTYPES[i], false);

            btn.setText(m.name);
            btn.setTag(m);
            mRgIndexType.addView(btn, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL));
            if (i == 0) {
                first = btn.getId();
                this.indexModel = m;
            }
            ids[i] = btn.getId();

        }

        // mRgIndexType.check(first);
    }


    private void initAdjustType() {

        int first = 0;
        adjustIds = new int[IndexModel.ADJUSTTYPES.length];
        for (int i = 0; i < IndexModel.ADJUSTTYPES.length; i++) {

            RadioButton btn = (RadioButton) View.inflate(getContext(), R.layout.indexitem, null);

            IndexModel indexModel = new IndexModel(IndexModel.ADJUSTTYPES[i], true);

            btn.setText(indexModel.name);
            btn.setTag(indexModel);


            mRgAdjustType.addView(btn, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL));
            if (i == 1) {
//                first = btn.getId();
                adjustModel = indexModel;
            }
            adjustIds[i] = btn.getId();

        }

//        mRgAdjustType.check(first);

    }

    public void setCheckedChange(IndexModel model,int checkedId){
        if (mOnIndexSelected != null) {
            if (model.reset) {
                this.adjustModel = model;
                mOnIndexSelected.onAdjustSelected(model);
            } else {
                this.indexModel = model;
                mOnIndexSelected.onIndexSelected(model);
            }
        }
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {


        View v = group.findViewById(checkedId);
        IndexModel model = (IndexModel) v.getTag();
//        System.out.println("checkedId:" + model.name);

        if (mOnIndexSelected != null) {
            if (model.reset) {
                this.adjustModel = model;
                mOnIndexSelected.onAdjustSelected(model);
            } else {
                this.indexModel = model;
                mOnIndexSelected.onIndexSelected(model);
            }
        }
    }

    public void changeAdjust(String type) {
        mRgAdjustType.check(adjustIds[getAdjustId(type)]);
    }

    public int getAdjustId(String type) {
        int ix = Arrays.asList(IndexModel.ADJUSTTYPES).indexOf(type);
        return Math.max(ix, 0);


    }

    public void changeIndex(String type) {

        selectIndex = getIndexId(type);

        mRgIndexType.check(ids[selectIndex]);

    }

    private int getIndexId(String type) {
        int ix = Arrays.asList(IndexModel.INDEXTYPES).indexOf(type);
        return Math.max(ix, 0);


    }


    public void changeIndex() {
        selectIndex++;

        selectIndex = selectIndex % ids.length;

        mRgIndexType.check(ids[selectIndex]);

    }

    public void setOnIndexSelected(OnIndexSelected l) {
        mOnIndexSelected = l;
    }


    public IndexModel getAdjustModel() {
        return adjustModel;
    }

    public IndexModel getIndexModel() {
        return indexModel;
    }

    public static interface OnIndexSelected {
        void onAdjustSelected(IndexModel model);

        void onIndexSelected(IndexModel model);
    }


}
